package com.spt.ws.ctrl;

import com.google.common.collect.Maps;
import com.google.gson.Gson;
import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;


@ControllerAdvice
public class MyExceptionHandler {
    Gson gson = new Gson();
    @ExceptionHandler({UnauthenticatedException.class})
    @ResponseBody
    String unAuthenticatedHandler(HttpServletRequest req, UnauthenticatedException e) {

        Map<String, Object> result = Maps.newHashMap();
        result.put("code", "-1");
        result.put("message", "您可以长时间没有进行操作，请重新登录!");

        return gson.toJson(result);
    }

    @ExceptionHandler({UnauthorizedException.class})
    @ResponseBody
    String unauthorizedHandler(HttpServletRequest req, UnauthorizedException e) {

        Map<String, Object> result = Maps.newHashMap();
        result.put("code", "-1");
        result.put("message", "您没有权限执行此操作，请使用授权用户登录!");

        return gson.toJson(result);
    }

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public  String otherErrorHandler(HttpServletRequest req, Exception e) {
        Map<String, Object> result = Maps.newHashMap();
        result.put("code", "-3");
        result.put("message", e.getClass().getName());

        return gson.toJson(result);
        ///return InternalResourceViewResolver.REDIRECT_URL_PREFIX + "/";
    }
}
